package com.me.shop.cart.action;

import org.apache.struts2.ServletActionContext;

import com.me.shop.cart.bean.Cart;
import com.me.shop.cart.bean.CartItem;
import com.me.shop.product.bean.Product;
import com.me.shop.product.service.ProductService;
import com.opensymphony.xwork2.ActionSupport;

public class CartAction extends ActionSupport {
	private ProductService productService;
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	private Integer pid;
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	
	private Integer count;
	public void setCount(Integer count) {
		this.count = count;
	}
	
	private Cart getCart() {
		Cart cart = (Cart)ServletActionContext.getRequest().getSession().getAttribute("cart");
		if(cart == null) {
			cart = new Cart();
			ServletActionContext.getRequest().getSession().setAttribute("cart", cart);
			return cart;
		}
		return cart;
	}
	

	public String addCart() {
		CartItem cartItem = new CartItem();
		cartItem.setCount(count);
		Product product = productService.findByPid(pid);
		cartItem.setProduct(product);
		Cart cart = getCart();
		cart.addCart(cartItem);
		return "addCart";  
	}
	
	public String clearCart() {
		Cart cart = getCart();
		cart.clearCart();
		return "clearCart";
	}
	
	public String removeCart() {
		Cart cart = getCart();
		cart.removeCart(pid);
		return "removeCart";
	}
	
	public String myCart() {
		
		return "myCart";
	}
	
	
	
	
	
}




























